/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.product.web.AddPurchaseSettingAction
   Module Description   :

   Date Created      : 2010/4/28
   Original Author   : afra.chou
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.product.web;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.product.entity.AddPurchaseSetting;
import com.yaodian100.ec.product.service.AddPurchaseSettingService;

/**
 * @author afra.chou
 * 
 */
public class AddPurchaseSettingAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8151785813321704429L;
	private static Logger LOGGER = Logger.getLogger(AddPurchaseSettingAction.class);
	private Long settingId;
	private String startDate;
	private String endDate;
	private String startTime;
	private String endTime;
	private String basePrice;
	private String checkResult;
	@Resource
	AddPurchaseSettingService addPurchaseSettingService;
	private SimplePager pager;
	private List<AddPurchaseSetting> addPurchaseSettingList;

	public String add() {
		clearErrorsAndMessages();
		setSettingId(null);
		setStartDate(null);
		setStartTime(null);
		setEndDate(null);
		setEndTime(null);
		setBasePrice(null);
		return "edit";

	}

	public List<AddPurchaseSetting> getAddPurchaseSettingList() {
		return addPurchaseSettingList;
	}

	public String getBasePrice() {
		return basePrice;
	}

	public String getCheckResult() {
		return checkResult;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getEndTime() {
		return endTime;
	}

	public SimplePager getPager() {
		return pager;
	}

	public Long getSettingId() {
		return settingId;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public String modify() {
		setCheckResult(null);
		DateFormat formatD = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		DateFormat formatT = new SimpleDateFormat("HH:mm");
		AddPurchaseSetting aps = addPurchaseSettingService.get(settingId);
		if (null != aps) {
			setSettingId(aps.getSettingId());
			setStartDate(formatD.format(aps.getStartDate()));
			setStartTime(formatT.format(aps.getStartDate()));
			setEndDate(formatD.format(aps.getEndDate()));
			setEndTime(formatT.format(aps.getEndDate()));
			setBasePrice(aps.getBasePrice().setScale(1, BigDecimal.ROUND_FLOOR).toString());

		}
		return "edit";
	}

	public void prepare() throws Exception {
		clearActionErrors();
		if (null == pager)
			pager = new SimplePager();
	}

	public String save() {
		DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
		Date sDate = null;
		Date eDate = null;
		try {
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(startTime))
				sDate = format.parse(startDate + " " + startTime + ":00");
			if (StringUtils.isNotBlank(endDate) && StringUtils.isNotBlank(endTime))
				eDate = format.parse(endDate + " " + endTime + ":59");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (addPurchaseSettingService.checkPeriod(sDate, eDate, settingId).size() > 0) {
			checkResult = "period_duplicate";
			return "edit";
		} else {
			if (settingId != null) {
				AddPurchaseSetting aps = addPurchaseSettingService.get(settingId);
				aps.setStartDate(sDate);
				aps.setEndDate(eDate);
				aps.setBasePrice(new BigDecimal(basePrice).setScale(1, BigDecimal.ROUND_FLOOR));

				addPurchaseSettingService.update(aps);
				return search();
			} else {
				AddPurchaseSetting aps = new AddPurchaseSetting();
				aps.setStartDate(sDate);
				aps.setEndDate(eDate);
				aps.setBasePrice(new BigDecimal(basePrice).setScale(1, BigDecimal.ROUND_FLOOR));
				addPurchaseSettingService.save(aps);
				return search();
			}
		}
	}

	public String search() {
		CommonCriteria criteria = new CommonCriteria();

		DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP_HH_MM);
		DateFormat formatD = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		DateFormat formatT = new SimpleDateFormat("HH:mm");
		Date sDate = null;
		Date eDate = null;

		int totalSize = 0;
		try {
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(startTime)) {
				sDate = format.parse(startDate + " " + startTime);
			} else {
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.MINUTE, 0);
				sDate = cal.getTime();

			}

			if (StringUtils.isNotBlank(endDate) && StringUtils.isNotBlank(endTime)) {
				eDate = format.parse(endDate + " " + endTime);
			} else {
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.SECOND, 59);
				cal.set(Calendar.MINUTE, 59);
				eDate = cal.getTime();
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Object[]> settingIds = addPurchaseSettingService.checkPeriod(sDate, eDate, null);
		ArrayList<Long> ids = new ArrayList<Long>();

		for (Iterator it = settingIds.iterator(); it.hasNext();) {
			ids.add(Long.valueOf(((BigDecimal) (it.next())).longValue()));
		}

		if (ids.size() > 0) {
			criteria.addIn("settingId", ids);

			totalSize = addPurchaseSettingService.getListSize(criteria).intValue();
			pager.setTotalSize(totalSize);
			addPurchaseSettingList = addPurchaseSettingService.getList(pager.getPageRecord() * pager.getCurrentPage(), pager
					.getPageRecord(), criteria, new String[] { "settingId asc" });

		} else {
			addPurchaseSettingList = null;
			pager.setTotalSize(0);
		}

		setStartDate(formatD.format(sDate));
		setStartTime(formatT.format(sDate));
		setEndDate(formatD.format(eDate));
		setEndTime(formatT.format(eDate));
		return "list";
	}

	public void setAddPurchaseSettingList(List<AddPurchaseSetting> addPurchaseSettingList) {
		this.addPurchaseSettingList = addPurchaseSettingList;
	}

	public void setBasePrice(String basePrice) {
		this.basePrice = basePrice;
	}

	public void setCheckResult(String checkResult) {
		this.checkResult = checkResult;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setSettingId(Long settingId) {
		this.settingId = settingId;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
}